package server.tets;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * ClassName MyClassLoader01
 *
 * @Description // TODO
 * @Author zhengkunqi
 * @Date 2020/8/6
 * Version 1.0
 **/
public class MyClassLoader extends ClassLoader {

    private String path;

    public MyClassLoader(String path) {
        this.path = path;
    }

    protected Class<?> findClass(String name) {
        try {
//            String path = "E:\\IDEA\\idea-workspace-lagou\\Minicat\\src\\main\\source\\webapps\\web-demo02\\WEB-INF\\classes\\com\\lagou\\test\\ResumeServlet02.class" ;
            FileInputStream in = new FileInputStream(path) ;
            ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            byte[] buf = new byte[1024] ;
            int len = -1 ;
            while((len = in.read(buf)) != -1){
                baos.write(buf , 0 , len);
            }
            in.close();
            byte[] classBytes = baos.toByteArray();
            return defineClass(classBytes , 0 , classBytes.length) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null ;
    }

}
